﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Jemgine.MapEditor.Operations
{
	internal class CloneEntityElement : Operation
	{
        Engine.Selection Selected;
        Engine.Selection ItemAdded;
		Data.EntityRecord Entity;

        internal CloneEntityElement(Engine.Selection Selected)
		{
			this.Selected = Selected;
		}

		internal override ReturnCode Do(EditorState ES)
		{
            Entity = ES.GetTopObject(Selected) as Data.EntityRecord;
            if (Entity == null) return ReturnCode.DoNotAddToUndoStack;

            Data.EntityElement ToClone = Entity.AttachedElements[Selected.MiddleIndex];
            Data.EntityElement NewElement = ToClone.Clone();

            ItemAdded = new Jemgine.Engine.Selection(Jemgine.Engine.Selection.Types.EntityElement, Selected.TopIndex,
                Entity.AttachedElements.Count, 0);
            Entity.AttachedElements.Add(NewElement);
            ES.RebuildNode(ItemAdded.TopIndex);
            ES.SelectItem(ItemAdded);
            return ReturnCode.AddToUndoStack;		}

		internal override void Undo(EditorState ES)
		{
			Entity.AttachedElements.RemoveAt(ItemAdded.MiddleIndex);
            ES.RebuildNode(ItemAdded.TopIndex);

            ES.SelectItem(new Engine.Selection(Engine.Selection.Types.MapElement, ItemAdded.TopIndex, 0, 0));
		}
	}
}
